package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminPicStyleDao;
import ex.com.jm.food.entity.PicStyle;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminPicStyleDaoImpl extends HibernateDaoSupport implements
		AdminPicStyleDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PicStyle> getPicStyles(final int initPage,final int pageRow,final String value,
			final String sort,final String order) {
		// TODO Auto-generated method stub
		Object picStyles=this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from PicStyle ps where 1=1 ";
				if(value!=null&&!value.equals("")){
					hql+=" and ps.styleValue like '%"+value+"%'";
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql+="order by "+order+" "+sort;;
				}else{
					hql +=" order by ps.styleValue asc";
				}
				
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<PicStyle>) picStyles;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public int picCount(final String value) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from PicStyle ps where 1=1 ";
				if(value!=null&&!value.equals("")){
					hql+=" and ps.styleValue like '%"+value+"%'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object savePicStyle(PicStyle picStyle) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(picStyle);
	}

	@Override
	public void updatePicStyle(PicStyle picStyle) {
		// TODO Auto-generated method stub
            this.getHibernateTemplate().update(picStyle);
	}

	@Override
	public PicStyle editPicStyle(Integer id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(PicStyle.class, id);
	}

	@Override
	public void delPicStyle(Integer id) {
		// TODO Auto-generated method stub
         this.getHibernateTemplate().delete(this.getHibernateTemplate().get(PicStyle.class, id));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PicStyle checkValue(final String value) {
		// TODO Auto-generated method stub
		PicStyle picStyle=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from PicStyle where styleValue="+value);
				return query.uniqueResult();
			}
		});
		return picStyle;
	}

}
